package com.sqtracker.recommender.controller;

import com.sqtracker.recommender.dto.RecommendationRequest;
import com.sqtracker.recommender.dto.RecommendationResponse;
import com.sqtracker.recommender.service.RecommendationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/recommendations")
@Tag(name = "推荐接口", description = "提供种子资源推荐的相关接口")
public class RecommendationController {

    private final RecommendationService recommendationService;

    @Autowired
    public RecommendationController(RecommendationService recommendationService) {
        this.recommendationService = recommendationService;
    }

    @GetMapping
    @Operation(summary = "获取个性化推荐", description = "根据用户ID获取个性化推荐结果")
    public ResponseEntity<RecommendationResponse> getRecommendations(
            @Parameter(description = "用户ID") @RequestParam String userId,
            @Parameter(description = "返回推荐数量") @RequestParam(required = false, defaultValue = "10") int count,
            @Parameter(description = "页码，从0开始") @RequestParam(required = false, defaultValue = "0") int page,
            @Parameter(description = "当前页面上下文") @RequestParam(required = false) String context) {

        RecommendationRequest request = new RecommendationRequest();
        request.setUserId(userId);
        request.setCount(count);
        request.setPage(page);
        request.setContextPage(context);

        RecommendationResponse response = recommendationService.getRecommendations(request);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/similar")
    @Operation(summary = "获取相似资源推荐", description = "根据指定的种子ID获取相似资源推荐")
    public ResponseEntity<RecommendationResponse> getSimilarRecommendations(
            @Parameter(description = "种子ID") @RequestParam String torrentId,
            @Parameter(description = "用户ID") @RequestParam(required = false) String userId,
            @Parameter(description = "返回推荐数量") @RequestParam(required = false, defaultValue = "6") int count) {

        RecommendationRequest request = new RecommendationRequest();
        request.setTorrentId(torrentId);
        request.setUserId(userId);
        request.setCount(count);

        RecommendationResponse response = recommendationService.getSimilarRecommendations(request);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/trending")
    @Operation(summary = "获取热门推荐", description = "获取当前热门的种子资源")
    public ResponseEntity<RecommendationResponse> getTrendingRecommendations(
            @Parameter(description = "返回推荐数量") @RequestParam(required = false, defaultValue = "10") int count,
            @Parameter(description = "分类ID") @RequestParam(required = false) String categoryId) {

        RecommendationRequest request = new RecommendationRequest();
        request.setCount(count);
        request.setCategoryId(categoryId);

        RecommendationResponse response = recommendationService.getTrendingRecommendations(request);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/stats")
    @Operation(summary = "获取推荐统计信息", description = "获取用户的推荐统计信息")
    public ResponseEntity<?> getRecommendationStats(
            @Parameter(description = "用户ID") @RequestParam String userId) {

        return ResponseEntity.ok(recommendationService.getRecommendationStats(userId));
    }

    @PostMapping("/feedback")
    @Operation(summary = "提交推荐反馈", description = "提交用户对推荐结果的反馈")
    public ResponseEntity<?> submitFeedback(
            @Parameter(description = "用户ID") @RequestParam String userId,
            @Parameter(description = "推荐ID") @RequestParam String recommendationId,
            @Parameter(description = "点击的种子ID") @RequestParam(required = false) String clickedTorrentId,
            @Parameter(description = "忽略的种子ID列表") @RequestParam(required = false) String[] ignoredTorrentIds) {

        recommendationService.recordFeedback(userId, recommendationId, clickedTorrentId, ignoredTorrentIds);
        return ResponseEntity.ok().build();
    }
}